using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;

namespace GizmoProject
{
    class DistanceDetection
    {
        public delegate void ObstacleDetected();
        public event ObstacleDetected ShouldStopEvent;

        private DateTime obstacleDetectedTime = DateTime.MinValue;
        System.Threading.Timer t = new System.Threading.Timer( new System.Threading.TimerCallback( TimerTick ), null, 0, 1 );
        private ExtendedTimer timer;

        private InterruptPort sensor;
        public bool shouldProceedForward;

        private static DistanceDetection instance;
        private static double TimeToWait = 1.5;
        private Random random;
        private bool shouldTurnLeft = false;

        public DistanceDetection( Cpu.Pin pin )
        {
            this.shouldProceedForward = false;
            this.timer = new ExtendedTimer( new System.Threading.TimerCallback( TimerTick ), null, ExtendedTimer.TimeEvents.Second );


            this.sensor = new InterruptPort( pin, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth );
            this.sensor.OnInterrupt += new NativeEventHandler( Sensor_OnInterrupt );
            random = new Random();
        }

        public static DistanceDetection Instance
        {
            get
            {
                if ( instance == null )
                    instance = new DistanceDetection( ( Cpu.Pin )FEZ_Pin.Digital.Di1 );

                return instance;
            }
        }

        public void Init()
        {
        }

        private static void TimerTick( object target )
        {
            if ( DistanceDetection.Instance.shouldProceedForward == true &&
                DateTime.Now.Subtract( DistanceDetection.Instance.obstacleDetectedTime ).Seconds > TimeToWait )
            {
                MovementManager.Instance.GoForward();
                DistanceDetection.Instance.shouldProceedForward = false;
            }
        }

        private void Sensor_OnInterrupt( uint data1, uint data2, DateTime time )
        {
            if ( this.shouldProceedForward == true )
                return;

            if ( data2 == 0 )
            {
                this.obstacleDetectedTime = DateTime.Now;
                this.shouldProceedForward = true;
                int r;
                if ( ( r = random.Next( 20 ) ) % 2 == 0 )
                {
                    shouldTurnLeft = false;
                    MovementManager.Instance.TurnRight();
                }
                else
                {
                    shouldTurnLeft = true;
                    MovementManager.Instance.TurnLeft();
                }
            }
            else
            {
                if ( DateTime.Now.Subtract( this.obstacleDetectedTime ).Seconds > TimeToWait )
                {
                    MovementManager.Instance.GoForward();
                }
                else
                {
                    if ( shouldTurnLeft == false )
                    {
                        MovementManager.Instance.TurnRight();
                    }
                    else
                    {
                        MovementManager.Instance.TurnLeft();
                    }
                }

            }
        }
    }
}
